package com.android.accessibility.dialog;

import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.android.accessibility.NickInfo;
import com.android.accessibility.R;
import com.android.accessibility.provider.DBConstant;
import com.android.accessibility.stat.StatManager;
import com.android.accessibility.utils.Log;

public class FunsDialog extends Dialog implements
android.view.View.OnClickListener {

    private ListView mListView;
    private NickAdapter mAdapter;
    private int mFuns;

    public FunsDialog(Context context, int funs) {
        super(context);
        mFuns = funs;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.funs_layout);
        mListView = (ListView) findViewById(R.id.listview);
        List<NickInfo> list = null;
        if (mFuns == DBConstant.FUNS_BLACK) {
            setTitle("黑名单");
            list = StatManager.get(getContext()).getBlackList();
        } else if (mFuns == DBConstant.FUNS_WHITE) {
            setTitle("白名单");
            list = StatManager.get(getContext()).getWhiteList();
        }
        if (list == null) {
            list = new ArrayList<NickInfo>();
        }
        mAdapter = new NickAdapter(getContext(), list);
        mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        mListView.setAdapter(mAdapter);
        View view = findViewById(R.id.cancel);
        view.setOnClickListener(this);
        view = findViewById(R.id.delete_select);
        view.setOnClickListener(this);
        view = findViewById(R.id.select_all);
        view.setOnClickListener(this);
    }

    class NickAdapter extends ArrayAdapter<NickInfo> {

        public NickAdapter(Context context, List<NickInfo> objects) {
            super(context, android.R.layout.simple_list_item_multiple_choice,
                    objects);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);
            TextView textView = (TextView) view
                    .findViewById(android.R.id.text1);
            textView.setTextColor(Color.BLACK);
            textView.setBackgroundColor(Color.WHITE);
            return view;
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.cancel) {
            dismiss();
        } else if (v.getId() == R.id.select_all) {
            selectedAll();
        } else if (v.getId() == R.id.delete_select) {
            deleteSelect();
            dismiss();
        }
    }

    public void selectedAll() {
        for (int i = 0; i < mAdapter.getCount(); i++) {
            mListView.setItemChecked(i, true);
        }
    }

    public void unSelectedAll() {
        mListView.clearChoices();
    }

    private void deleteSelect() {
        SparseBooleanArray positions = mListView.getCheckedItemPositions();
        if (positions == null) {
            return;
        }
        List<Integer> ids = new ArrayList<Integer>();
        for (int i = 0; i < mAdapter.getCount(); i++) {
            if (positions.get(i, false)) {
                ids.add(mAdapter.getItem(i).getId());
            }
        }
        Log.d(Log.TAG, "ids : " + ids);
        StatManager.get(getContext()).deleteByIds(ids);
    }
}
